package 网上的;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManger02 {
    public static void main(String[] args) {
        //创建对象集合
        ArrayList<Student> list = new ArrayList<>();

        while (true) {
            System.out.println("-------------welcome----------");
            System.out.println("1 add");
            System.out.println("2 del");
            System.out.println("3 edit");
            System.out.println("4 list");
            System.out.println("5 quit");
            System.out.println("please chose:");
            Scanner sc = new Scanner(System.in);
            String line = sc.nextLine();

            switch (line){
                case "1":
                    //System.out.println("add");
                    addStudent(list);
                    break;
                case "2":
                    deleteStudent(list);
                    break;
                case "3":
                    updateStudent(list);
                    break;
                case "4":
                    findAllStudent(list);
                    break;
                case "5":
                    System.out.println("thanks");
                    System.exit(0); //java虚拟机的退出

            }

        }
    }

    //添加学生的方法
    public static void addStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println();
        System.out.println("学号");
        String sid = sc.nextLine();
        System.out.println("姓名");
        String name = sc.nextLine();
        System.out.println("年龄");
        String age = sc.nextLine();
        System.out.println("地址");
        String address = sc.nextLine();

        //创建一个对象
        Student s = new Student(sid, name, age, address);

        //添加到数组中
        list.add(s);

        System.out.println("添加成功");
    }

    public static void deleteStudent(ArrayList<Student> list) {
        System.out.println("请输入你要删除的学号：");
        Scanner sc = new Scanner(System.in);
        String id = sc.next();
        //遍历这个数组
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            if (id.equals(student)) {
                list.remove(i);
                break;
            }
        }
        System.out.println("删除成功");
    }

    //修改学生的信息
    public static void updateStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入你要修改的学号");
        String sid = sc.nextLine();
        System.out.println("姓名");
        String name = sc.nextLine();
        System.out.println("年龄");
        String age = sc.nextLine();
        System.out.println("地址");
        String address = sc.nextLine();
        //创建对象
        Student s = new Student(sid, name, age, address);

        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            if (sid.equals(student)) {
                list.set(i, s);
                break;
            }
        }
    }

    //查看学生的信息
    public static void findAllStudent(ArrayList<Student> list) {
        if (list.size() == 0) {
            System.out.println("暂时没有学生信息");
        }else {
            System.out.println("学号      姓名      年龄      居住地     ");
            for (int i = 0; i < list.size(); i++) {
                Student student = list.get(i);
                String address = student.getAddress();
                String age = student.getAge();
                String name = student.getName();
                String sid = student.getSid();
                System.out.println(sid + "       " + name + "        " + age + "     " + address);
            }
        }

    }
}
